/*
 * Copyright (c) 2007-2021 西安交通信息投资营运有限公司 版权所有
 */

package com.xci.xajtfb.gjzn.service;

import com.xci.xajtfb._core.IGpsConvert;
import com.xci.xajtfb._core.PositionUtil;
import com.xci.xajtfb.gjzn._core.GjznBaseService;
import com.xci.xajtfb.gjzn.dao.GjznDao;
import com.xci.xajtfb.gjzn.entity.BusGpsPosition;
import com.xci.xajtfb.gjzn.entity.BusGpsTrack;
import com.xci.xajtfb.gjzn.filter.GpsPositionFilter;
import com.xci.xajtfb.gjzn.filter.GpsTrackFilter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 公交智能服务
 * @author 吕艳阳 lvyanyang@xci96716.com
 * @since 2021-02-07 14:10:10
 */
@Service
public class GjznService extends GjznBaseService {
    /** 公交智能数据层对象 */
    @Resource private GjznDao gjznDao;

    /**
     * 查询线路信息列表
     */
    public List<Map<String, String>> selectRouteList(String name) {
        return gjznDao.selectRouteList(name);
    }

    /**
     * 根据线路主键查询车辆信息
     */
    public List<Map<String, String>> selectBusListByRouteId(String routeId) {
        return gjznDao.selectBusListByRouteId(routeId);
    }

    /**
     * 查询车辆最后Gps位置
     */
    public List<BusGpsPosition> selectBusLastGpsPosition(GpsPositionFilter filter) {
        var list = gjznDao.selectBusLastGpsPosition(filter);
        ConvertGps(list);
        return list;
    }

    /**
     * 查询车辆Gps轨迹
     */
    public List<BusGpsTrack> selectBusGpsTrack(GpsTrackFilter filter) {
        var list = gjznDao.selectBusGpsTrack(filter);
        ConvertGps(list);
        return list;
    }

    private void ConvertGps(List<? extends IGpsConvert> list) {
        for (IGpsConvert item : list) {
            var res = PositionUtil.gps84_To_Gcj02(Double.parseDouble(item.getLongitude()), Double.parseDouble(item.getLatitude()));
            if (res != null) {
                item.setLongitudeValue(res.getWgLon());
                item.setLatitudeValue(res.getWgLat());
            }
        }
    }
}